package org.berlin.octane.compiler.app;

import org.berlin.octane.compiler.lang.IntegerNode;
import org.berlin.octane.compiler.lang.PlusNode;
import org.berlin.octane.compiler.lang.SimpleAdderVisitor;
import org.berlin.octane.compiler.lang.VisitorBaseLexer;
import org.berlin.octane.compiler.lang.VisitorSimpleLexer;
import org.berlin.octane.compiler.lang.VisitorToken;

public class Main {

  public static IntegerNode i(final int i) {
    return new IntegerNode(new VisitorToken(VisitorToken.INTEGER, String.valueOf(i)));
  }

  public static PlusNode p(final int a, final int b) {
    return new PlusNode(new VisitorToken(VisitorToken.PLUS, "+"), i(a), i(b));
  }

  
  /**
   * Main entry point.
   * 
   * @param args String[]
   */
  public static void main(String[] args) {        
        final String program2 = "(+ 100 200)";
        final VisitorSimpleLexer lexer2 = new VisitorSimpleLexer(program2);
        VisitorToken lastToken = lexer2.nextToken();        
        PlusNode lastPlusNode = null;
        IntegerNode lastIntNode; 
        System.out.println("Parsing input program : " + program2);
        while (lastToken.getType() != VisitorBaseLexer.EOF_TYPE) {                        
            System.out.println("LastToken: " + lastToken);            
            lastToken = lexer2.nextToken();
            if (VisitorToken.PLUS == lastToken.getType()) {                                
                lastToken = lexer2.nextToken();
                IntegerNode arg1 = null;
                IntegerNode arg2 = null;
                if (VisitorToken.INTEGER == lastToken.getType()) {
                    lastIntNode = i(Integer.parseInt(lastToken.getText()));
                    arg1 = lastIntNode; 
                }
                lastToken = lexer2.nextToken();
                if (VisitorToken.INTEGER == lastToken.getType()) {
                    lastIntNode = i(Integer.parseInt(lastToken.getText()));
                    arg2 = lastIntNode;
                }
                lastPlusNode = new PlusNode(new VisitorToken(VisitorToken.PLUS, "+"), arg1, arg2);
            } // End of If //
            
        } // End of the While //
        final SimpleAdderVisitor visitor = new SimpleAdderVisitor();        
        visitor.visit(lastPlusNode);
        System.out.println("Last Result : " + visitor.getLastResult());
   }
  
} // End of the class //
